package com.ycshihuo.shihuo.core.page;



import com.ycshihuo.shihuo.util.PublicConstants;

import java.util.List;

public class Page<T> {

    private int pageNum = 1; // 当前页, 默认为第1页
    private int pageSize = PublicConstants.PAGE_SIZE; // 每页记录数
    private long totalCount = -1; // 总记录数, 默认为-1, 表示需要查询
    private int totalPage = -1; // 总页数, 默认为-1, 表示需要计算

    protected List<T> resultList; // 当前页记录List形式

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        computeTotalPage();
    }

    public long getTotalCount() {
        return totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        computeTotalPage();
    }

    protected void computeTotalPage() {
        if (getPageSize() > 0 && getTotalCount() > -1) {
            this.totalPage = (int) (getTotalCount() % getPageSize() == 0 ? getTotalCount() / getPageSize()
                    : getTotalCount() / getPageSize() + 1);
        }
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder().append("Page [pageNum=").append(pageNum).append(", pageSize=")
                .append(pageSize).append(", totalCount=").append(totalCount < 0 ? "null" : totalCount)
                .append(", totalPage=").append(totalPage < 0 ? "null" : totalPage).append(", curPageObjects=")
                .append(resultList == null ? "null" : resultList.size()).append("]");
        return builder.toString();
    }

}
